<template>
  <div class="body">
    <div class="top">
      <span>服务文本</span>
      <el-button type="primary" @click="open">添加</el-button>
    </div>
    <div class="box">
      <div class="card" v-if="goods.length">
        <div class="item" v-for="(item,idx) in goods" :key="idx" @click="deleteId(idx)">
          <div class="img" v-if="item.img">
            <img :src="item.img" alt="">
          </div>
          <div class="info">
            <div class="title">{{ item.title }}</div>
            <div class="cnt">{{ item.cnt }}</div>
          </div>
        </div>
      </div>
      <el-empty style="margin:0 auto" description="暂无数据" image-size="200px" v-if="!goods.length"></el-empty>
    </div>

    <el-dialog title="文本信息" v-model="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="图片" :label-width="formLabelWidth">
          <file @getName="getName1"></file>
        </el-form-item>


        <el-form-item label="内容" :label-width="formLabelWidth">
          <el-input v-model="form.cnt" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="finish">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="查看信息" v-model="disalogFormVisible">
      <el-form>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <span>{{ goods[id]?.title }}</span>
        </el-form-item>

        <el-form-item label="图片" :label-width="formLabelWidth">
          <span>{{ goods[id]?.img }}</span>
        </el-form-item>

        <el-form-item label="内容" :label-width="formLabelWidth">
          <span>{{ goods[id]?.cnt }}</span>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Delete">删除</el-button>
        <el-button type="primary" @click="edit">修改</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script setup>
import {ref, reactive, inject, onMounted} from "vue";
import file from '../components/upload'
import copy from '../utils/copy'
import {ElMessage} from "element-plus";
// 引入
const axios = inject('$axios')
const formLabelWidth = ref(200)
const dialogFormVisible = ref(false)
const close = () => dialogFormVisible.value = false
const open = () => dialogFormVisible.value = true
const id = ref(null)
const disalogFormVisible = ref(false)

const goods = reactive(
    [])

const form = reactive({
  title: "",
  img: "",
  cnt: ''

})
const edit = () => {
  disalogFormVisible.value = false
  dialogFormVisible.value = true
  copy(goods[id.value], form)

}
const baseUrl = inject("$baseURL")
const getName1 = (value) => {
  form.img = baseUrl + value.value
}


const finish = () => {


  if (1) {
    axios.post('/text', form).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  } else alert("请完整填写信息")

  for (var obj in form) {
    form[obj] = ''
  }
  dialogFormVisible.value = false
  window.location.reload()

}


onMounted(() => {
  axios.get("text/list").then(res => {
    copy(res.data?.data, goods)
    console.log(res)
  })
})
const deleteId = (i) => { 1
  id.value = i
  disalogFormVisible.value = true

}
const Delete = () => {

  axios.get("text/delete?id=" + goods[id.value].id).then(res => {
     ElMessage.success(res.data?.data)

  }).catch(err => {

  })
  window.location.reload()
  disalogFormVisible.value = false

}
</script>

<style scoped lang="less">

.dialog-footer {
  margin: 0 auto;
  text-align: center;
}

.top {
  margin: 20px auto;
  background-color: rgba(255, 255, 255, .9);
  width: 250px;
  height: 40px;
  border-radius: 20px;
  line-height: 40px;
  text-align: start;
  text-indent: 2em;
  box-shadow: 1px 1px 10px #bacfe7;

  span {
    margin: auto 20px;
  }
}

.box {
  margin: 10px auto;
  display: flex;
  flex-direction: column;
  align-items: start;
  width: 80%;
  background-color: #fff;
  min-height: 500px;
  border-radius: 12px;
  box-shadow: 0 0 7px aliceblue;

  .card {
    padding: 10px;
    width: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: row;
    justify-content: start;
    flex-wrap: wrap;

    .item {
      width: 30%;
      height: 120px;
      border: 1px solid #EBEDF0;
      display: flex;
      flex-direction: row;
      justify-content: start;
      align-items: center;
      margin: 10px;
      text-align: start;

      .img {
        width: 100px;
        height: 100px;

        img {
          width: 100%;
          height: 100%;

        }
      }

      .info {
        height: 100%;
        flex: 1;
        padding: 10px;
        display: flex;
        flex-direction: column;

        .title {
          height: 50px;
          font-weight: 500;
          letter-spacing: 3px;
          font-size: 20px;
          color: #000;
        }

        .cnt {
          color: #606266;
          font-weight: 400;
          font-size: 17px;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          overflow: hidden;
          text-overflow: ellipsis;
          -webkit-box-orient: vertical;
          word-break: break-all;
        }
      }
    }

    .item:hover {
      background-color: #fff;
      box-shadow: 0 0 10px aliceblue;
      transform: translateX(3px) translateY(-3px);
    }
  }
}
</style>